package compareResult.datasetCompare;

import com.supermap.data.DatasetImage;
import com.supermap.data.GeoRegion;

import java.util.Random;

/**
 * Created by hanyz on 2016/11/29.
 */
public class DatasetImageComparer extends DatasetComparer {
    public boolean compare(DatasetImage expDataset, DatasetImage actualDataset) throws Exception {
        super.compareCommon(expDataset, actualDataset);
        //金字塔
        if (expDataset.getHasPyramid() != actualDataset.getHasPyramid()) {
            compareError += "expDataset HasPyramid:" + expDataset.getHasPyramid() + "\nactualDataset HasPyramid:" + actualDataset.getHasPyramid();
            throw new Exception(compareError);
        }
        //像素分块类型
        if (!expDataset.getBlockSizeOption().equals(actualDataset.getBlockSizeOption())) {
            compareError += "expDataset BlockSizeOption:" + expDataset.getBlockSizeOption() + "\nactualDataset BlockSizeOption:" + actualDataset.getBlockSizeOption();
            throw new Exception(compareError);
        }
        //波段个数
        if (expDataset.getBandCount() != actualDataset.getBandCount()) {
            compareError += "expDataset BandCount:" + expDataset.getBandCount() + "\nactualDataset BandCount:" + actualDataset.getBandCount();
            throw new Exception(compareError);
        }
        //波段属性
        for (int i = 0; i < expDataset.getBandCount(); i++) {
            //像素格式
            if (!expDataset.getPixelFormat(i).equals(actualDataset.getPixelFormat(i))) {
                compareError += "expDataset Band" + i + "PixelFormat:" + expDataset.getPixelFormat(i) + "\nactualDataset Band" + i + "PixelFormat:" + actualDataset.getPixelFormat(i);
                throw new Exception(compareError);
            }
            //极值
            double expValue;
            double actualValue;
            expValue = expDataset.getMaxValue(i);
            actualValue = actualDataset.getMaxValue(i);
            if (!CompareUtilities.compareDouble(expValue, actualValue, tolerance)) {
                compareError += "expDataset Band" + i + "MaxValue:" + expValue + "\nactualDataset Band" + i + "MaxValue:" + actualValue;
                throw new Exception(compareError);
            }
            expValue = expDataset.getMinValue(i);
            actualValue = actualDataset.getMinValue(i);
            if (!CompareUtilities.compareDouble(expValue, actualValue, tolerance)) {
                compareError += "expDataset Band" + i + "MinValue:" + expValue + "\nactualDataset Band" + i + "MinValue:" + actualValue;
                throw new Exception(compareError);
            }
            //无值
            expValue = expDataset.getNoData(i);
            actualValue = actualDataset.getNoData(i);
            if (!CompareUtilities.compareDouble(expValue, actualValue, tolerance)) {
                compareError += "expDataset Band" + i + "NoData:" + expValue + "\nactualDataset Band" + i + "NoData:" + actualValue;
                throw new Exception(compareError);
            }

        }
        //行列数
        if (expDataset.getWidth() != actualDataset.getWidth()) {
            compareError += "expDataset width:" + expDataset.getWidth() + "\nactualDataset width:" + actualDataset.getWidth();
            throw new Exception(compareError);
        }
        if (expDataset.getHeight() != actualDataset.getHeight()) {
            compareError += "expDataset height:" + expDataset.getHeight() + "\nactualDataset height:" + actualDataset.getHeight();
            throw new Exception(compareError);
        }
        //显示范围
        GeoRegion expClipRegion = expDataset.getClipRegion();
        GeoRegion actualClipRegion = actualDataset.getClipRegion();
        EqualAssert.isGeometryEqual(expClipRegion, actualClipRegion, tolerance);

        //比较格子..
        int width = expDataset.getWidth();
        int height = expDataset.getHeight();
        int errorCount = 0;
        int compareCount = 0;
        //网格小于5000顺序间隔比较
        if (width * height * expDataset.getBandCount() < 5000) {
            for (int band = 0; band < expDataset.getBandCount(); band++) {
                for (int i = 0; i < width; i += 2) {
                    for (int j = 0; j < height; j += 2) {
                        double d1 = expDataset.getValue(i, j, band);
                        double d2 = actualDataset.getValue(i, j, band);
                        compareCount++;
                        if (Math.abs(d1 - d2) > tolerance)
                            errorCount++;
                    }
                }
            }
        }
        //网格大于2000，随机比较50个点
        else {
            Random random = new Random();
            for (int band = 0; band < expDataset.getBandCount(); band++) {
                for (int i = 0; i < 50; i++) {
                    int x = random.nextInt(width);
                    int y = random.nextInt(height);
                    double d1 = expDataset.getValue(x, y, band);
                    double d2 = actualDataset.getValue(x, y, band);
                    compareCount++;
                    if (Math.abs(d1 - d2) > tolerance)
                        errorCount++;
                }
            }
        }
        //如果相同值的点大于百分之95则任务对比通过
        if ((double) (errorCount) / (double) compareCount >= 0.05) {
            compareError += "cell diff rate:" + (double) (errorCount) / (double) compareCount;
            throw new Exception(compareError);
        }
        return true;
    }
}
